package com.tpu.daoclasses;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.tpu.bdclasses.Event;
/**
* The Data Access Object for managing the persistent Events.
*/
public class SimpleEventDao {
Log log = LogFactory.getLog(SimpleEventDao.class);
private Session session;
private Transaction tx;
public SimpleEventDao() {
//HibernateFactory.buildIfNeeded();
}
public void create(Event event)
throws DataAccessLayerException {
try {
startOperation();
session.save(event);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
public void delete(Event event)
throws DataAccessLayerException {
try {
startOperation();
session.delete(event);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
public Event find(Long id) throws DataAccessLayerException {
Event event = null;
try {
startOperation();
event = (Event) session.load(Event.class, id);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return event;
}

public void update(Event event)
throws DataAccessLayerException {
try {
startOperation();
session.update(event);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
private void handleException(HibernateException e)
throws DataAccessLayerException {
HibernateFactory.rollback(tx);
throw new DataAccessLayerException(e);
// Alternatively, you could just rethrow, like so...
// throw e;
}
private void startOperation() throws HibernateException {
session = HibernateFactory.openSession();
tx = session.beginTransaction();
}
}

